﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using Windows.ApplicationModel.Core;
using Windows.UI.ViewManagement;
using AdRotatorXNA;
using Microsoft.Xna.Framework.Content;

namespace AdRotatorExampleXNA
{
    class GameController : IDisposable
    {
        public static GameController Instance;

        private SharedGraphicsDeviceManager _graphicsManager;

        private GameTimer _timer;

        public GraphicsDevice GraphicsDevice { get; private set; }

        public GameServiceContainer Services { get; private set; }

        public ContentManager Content { get; private set; }


        public GameController()
        {
            Instance = this;

            // Create the service provider.
            Services = new GameServiceContainer();

            // Setup the graphics manager.
            _graphicsManager = new SharedGraphicsDeviceManager();
            Services.AddService(typeof(IGraphicsDeviceService), _graphicsManager);

            Content = new ContentManager(Services);

            _graphicsManager.PreferredBackBufferFormat = SurfaceFormat.Color;

            _graphicsManager.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;

            _graphicsManager.PreferredDepthStencilFormat = DepthFormat.Depth16;
        }

        public void Initialize()
        {

            GraphicsDevice = AdRotatorXNAComponent.device = AdRotatorXNAComponent.Current.GraphicsDevice = _graphicsManager.GraphicsDevice;

            AdRotatorXNAComponent.Current.Initialize();

            //Specify where you want the Ad to display (based on the top left corner of the Ad)
            AdRotatorXNAComponent.Current.AdPosition = new Vector2(0, 720);

            //Optionally specify your own House Ad which will display if there is no network
            AdRotatorXNAComponent.Current.DefaultHouseAdImage = Content.Load<Texture2D>(@"AdRotatorDefaultAdImage");

            //Event handler to do something should the user click on your Default House Ad
            AdRotatorXNAComponent.Current.DefaultHouseAdClick += new AdRotatorXNAComponent.DefaultHouseAdClickEventHandler(Current_DefaultHouseAdClick);

            //Optionally specify the slide (popup) direction for the Ad
            AdRotatorXNAComponent.Current.SlidingAdDirection = SlideDirection.None;

            //Optionally Set the local configuration file used to set the default Ad Locations
            AdRotatorXNAComponent.Current.DefaultSettingsFileUri = "defaultAdSettings.xml";

            //Optionally set a URL from where to pull the configuration file down remotely each time the game is run
            //AdRotatorXNAComponent.Current.SettingsUrl = "http://mysite.com/defaultAdSettings.xml";

            _timer = new GameTimer();
            _timer.UpdateInterval = TimeSpan.Zero;
            _timer.Update += (o, a) => Update(new GameTime(a.TotalTime, a.ElapsedTime));
            _timer.Draw += (o, a) => Draw(new GameTime(a.TotalTime, a.ElapsedTime));
            _timer.Start();

        }

        void Current_DefaultHouseAdClick()
        {
            try
            {
                var md = new Windows.UI.Popups.MessageDialog("You clicked on my Ad, thanks very much");
                md.ShowAsync().AsTask();
            }
            catch { }
        }

        public void Dispose()
        {
        }

        public void Update(GameTime gameTime)
        {
            AdRotatorXNAComponent.Current.Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            AdRotatorXNAComponent.Current.Draw(gameTime);
        }

    }
}
